package com.raising.framework.entity;

import com.google.common.collect.Lists;

import java.util.List;
import java.util.regex.Pattern;

/**
 * 分页工具
 * @author gaoy
 */
public class Pager<T> {
	/** 当前页码 */
	private int pageNo = 1;
	/** 页面大小 */
	private int pageSize = 10;
	/** 总页数 */
	private int totalPage;
	/** 总条数 */
	private int totalCount;

	private List<T> list = Lists.newArrayList();
	private T entity;

	/** 排序字段 */
	private String orderBy;
	/** 分页字段 */
	private String limitBy;
	/** 查询关键字 */
	private String key;

	/**
	 * 空构造方法
	 */
	public Pager() { }

	public String getOrderBy() {
		// SQL过滤，防止注入
		String reg = "(?:')|(?:--)|(/\\*(?:.|[\\n\\r])*?\\*/)|" + "(\\b(select|update|and|or|delete|insert|trancate|char|into|substr|ascii|declare|exec|count|master|into|drop|execute)\\b)";
		if (Pattern.compile(reg, Pattern.CASE_INSENSITIVE).matcher(orderBy).find()) {
			return "";
		}
		return orderBy;
	}

	public void setOrderBy(String orderBy) {
		this.orderBy = orderBy;
	}

	public String getLimitBy() {
		this.limitBy = this.pageNo * this.pageSize + "," + this.pageSize;
		return this.limitBy;
	}

	public void setLimitBy(String limitBy) {
		this.limitBy =limitBy;
	}

	public String getKey() {
		return key;
	}

	public void setKey(String key) {
		this.key = key;
	}

	public int getPageNo() {
		return pageNo;
	}

	public void setPageNo(int pageNo) {
		this.pageNo = pageNo;
	}

	public int getPageSize() {
		return pageSize;
	}

	public void setPageSize(int pageSize) {
		this.pageSize = pageSize;
	}

	public int getTotalPage() {
		return totalPage;
	}

	public void setTotalPage(int totalPage) {
		this.totalPage = totalPage;
	}

	public int getTotalCount() {
		return totalCount;
	}

	public void setTotalCount(int totalCount) {
		this.totalCount = totalCount;
	}

	public List<T> getList() {
		return list;
	}

	public void setList(List<T> list) {
		this.list = list;
	}

	public T getEntity() {
		return entity;
	}

	public void setEntity(T entity) {
		this.entity = entity;
	}
}
